package com.myph.phctrl_t;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.phctrl_t.R;
import com.myph.model.ClientSocketNew;
import com.myph.model.Util;

public class NextActivity extends Activity implements OnClickListener{
    
    private String str_response;
    private String str_request;
    private EditText edt_port,edt_ip;
    private Button btn_connect;
    private String ip;
    private String port;

    public ClientSocketNew clientSocketNew;
    long prevTime = 0;
    
    private long prevClickTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_next);

        edt_port = (EditText) findViewById(R.id.edt_port);
        edt_ip = (EditText) findViewById(R.id.edt_ip);
        btn_connect = (Button) findViewById(R.id.btn_connect);

        btn_connect.setOnClickListener(this);

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // switch (msg.what) {

            switch (msg.what) {
           
            case 10:
                
                ip = edt_ip.getText().toString().trim();
                port = edt_port.getText().toString().trim();
                
                //TODO:link test
//                ip = "10.10.100.254";
//                port = "8899";
                
                Pattern pa = Pattern
                        .compile("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
                Matcher ma = pa.matcher(ip);
                if (ma.matches() == false) {
                    Toast.makeText(getApplicationContext(), "ip invalid", Toast.LENGTH_SHORT).show();
                    return;
                }
                int int_port = 0;
                try {
                    int_port = Integer.parseInt(port);
                }catch(Exception e){
                    Toast.makeText(getApplicationContext(), "port invalid", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(int_port<0 || int_port > 65535){
                    Toast.makeText(getApplicationContext(), "port invalid", Toast.LENGTH_SHORT).show();
                    return;
                }
                
                Util.saveSharedPref(getApplicationContext(), "ip", ip);
                Util.saveSharedPref(getApplicationContext(), "port", port);
                clientSocketNew = new ClientSocketNew(ip,port,handler);
                clientSocketNew.setTestConnectionMode();
                clientSocketNew.start();
                break;
            case 1001:
                Toast.makeText(getApplicationContext(), "connect ok!", Toast.LENGTH_SHORT).show();
                Intent intent1 = new Intent(NextActivity.this,SelectFunctionActivity.class);
                startActivity(intent1);
                break;
            case 1002:
                Toast.makeText(getApplicationContext(), "fail to connect", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getApplicationContext(), "handler error!",
                        Toast.LENGTH_LONG).show();
                break;
            }

        }
    };
    @Override
    public void onClick(View v) {
    	long pressGap = System.currentTimeMillis() - prevClickTime;
		if(pressGap < 1000){
			return;
		}else{
			prevClickTime = System.currentTimeMillis();
		}
        switch (v.getId()) {
        case R.id.btn_connect:
        	long timeGap = System.currentTimeMillis() - prevTime;
        	if(timeGap < 1000)return;
        	
            Message message2 = new Message();
            message2.what = 10;
            handler.sendMessage(message2);
            break;
        default:
            break;
        }
    }
}
